package com.itboye.ihomebank.adapter;

import android.content.Context;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.ViewTreeObserver;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.MyApplcation;
import com.itboye.ihomebank.bean.HouseListBean.HouseItem;
import com.itboye.ihomebank.constants.NetPublicConstant;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.util.XImageLoader;

import java.util.List;

import static com.itboye.ihomebank.R.id.home_danjia;


/**
 * 首页中显示的房屋列表
 */
public class HomeAdapter extends BaseAdapter {
    private Context context;

    private List<HouseItem> list;
    private boolean tagWidth;
    public HomeAdapter(Context context, List<HouseItem> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder holder = null;
        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(
                    R.layout.item_home_adapter, null);
            holder.home_adapter = (ImageView) view
                    .findViewById(R.id.home_adapter);
            holder.home_title = (TextView) view.findViewById(R.id.home_title);
            holder.home_xq = (TextView) view.findViewById(R.id.home_xq);
            holder.tag = (LinearLayout) view.findViewById(R.id.tag);
            holder.home_money = (TextView) view.findViewById(R.id.home_money);
            holder.home_danjia = (TextView) view.findViewById(home_danjia);
            holder.danjia = (TextView) view.findViewById(R.id.danjia);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        XImageLoader.load(NetPublicConstant.IMAGE_URL
                + list.get(position).getMainImg()+ SPContants.SHUIYIN, holder.home_adapter);
        Log.d("====woter",NetPublicConstant.IMAGE_URL
                + list.get(position).getMainImg()+ SPContants.SHUIYIN);
//        Glide.with(context).load(NetPublicConstant.IMAGE_URL
//                + list.get(position).getMainImg()+ SPContants.SHUIYIN).priority(Priority.HIGH).into(holder.home_adapter);
        holder.home_title.setText(list.get(position).getRentTyoe() + "·"
                + list.get(position).getCommunity()
                + list.get(position).getHouseRoom() + "室"
                + list.get(position).getHouseHall() + "厅"
                + list.get(position).getToilet() + "卫");
        holder.home_xq.setText(list.get(position).getHouseArea()
                + "㎡·" + list.get(position).getOritation());
        holder.danjia.setText(Integer.parseInt(list.get(position).getRent()) / 100
                + "元/月");
        if(list.get(position).getAreaNone().equals("")){
            holder.home_money.setText(Html.fromHtml(list.get(position).getArea()));
        }else{
            holder.home_money.setText(Html.fromHtml(list.get(position).getArea() + "-"
                    + list.get(position).getAreaNone()));
        }

        if (list.get(position).getEntrustMoney() != null) {
            holder.home_danjia.setVisibility(View.VISIBLE);
            holder.home_danjia.setText(Double.parseDouble(list.get(position).getEntrustMoney()) / 100 + "元");
        } else {
            holder.home_danjia.setVisibility(View.GONE);
        }
        String str[] = null;
        holder.tag.removeAllViews();

        if (list.get(position).getTagNames() != null) {

            if (list.get(position).getTagNames().equals("")) {
              //  holder.tag.setVisibility(View.GONE);
            } else if (list.get(position).getTagNames().contains(",")) {
                str = list.get(position).getTagNames().split(",");
                for (int i = 0; i < str.length; i++) {
                    View innerView = LayoutInflater.from(MyApplcation.ctx)
                            .inflate(R.layout.item_biaoqian, null);
                    TextView btn_name = (TextView) innerView
                            .findViewById(R.id.btn_name);
                    btn_name.setText(str[i]);
                    btn_name.setTextSize(14);
                    LayoutParams layoutParams = new LayoutParams(
                            LayoutParams.WRAP_CONTENT,
                            LayoutParams.WRAP_CONTENT);
                    layoutParams.setMargins(5, 5, 5, 5);
                    if(i<=2){
                        holder.tag.addView(innerView, layoutParams);
                    }

                }

            } else {
                View innerView = LayoutInflater.from(MyApplcation.ctx).inflate(
                        R.layout.item_biaoqian, null);
                TextView btn_name = (TextView) innerView
                        .findViewById(R.id.btn_name);
                btn_name.setText(list.get(position).getTagNames());
                MarginLayoutParams layoutParams = new MarginLayoutParams(
                        MarginLayoutParams.WRAP_CONTENT,
                        MarginLayoutParams.WRAP_CONTENT);
                layoutParams.setMargins(5, 5, 5, 5);
                holder.tag.addView(innerView, layoutParams);
            }
        }

        return view;
    }


    class ViewHolder {
        ImageView home_adapter;// 图片
        TextView home_title;// 标题
        TextView home_xq;// 详情
        TextView home_biaoqian_01, home_biaoqian_02, home_biaoqian_03;// 标签
        TextView home_money;// 价格
        TextView home_danjia;// 单价
        LinearLayout tag;
        TextView danjia;
    }

//    //获取控件的宽度
//    public int getTagWidth(final View view,final boolean hasMeasured){
//        ViewTreeObserver vto =view.getViewTreeObserver();
//        int mesWidth;
//        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
//            public boolean onPreDraw() {
//                if (hasMeasured) {
//                    mesWidth = view.getMeasuredWidth();
//                    //获取到宽度和高度后，可用于计算
//                }
//                return true;
//            }
//
//        });
//        Log.d("宽度==",mesWidth+"");
//         return  mesWidth;
//    }
}
